package user.zc.execute;


import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;

import static java.sql.ResultSet.CONCUR_READ_ONLY;
import static java.sql.ResultSet.TYPE_FORWARD_ONLY;

/**
 * @author zhouchuang
 * @date 2023/8/9 9:34
 */
public class ColumnParse implements Function<Connection, List<String>> {

    private String tableName;
    public ColumnParse(String tableName){
        this.tableName = tableName;
    }
    @Override
    public List<String> apply(Connection conn) {
        List<String> columns = new ArrayList();
        try {
            Statement statement = conn.createStatement(TYPE_FORWARD_ONLY, CONCUR_READ_ONLY);
            String str = MessageFormat.format(
                    "select column_name,data_type,column_comment,numeric_precision,numeric_scale,character_maximum_length,is_nullable nullable from information_schema.columns where table_name = ''{0}'' order By ORDINAL_POSITION",
                    new Object[] { tableName });
            ResultSet localResultSet = statement.executeQuery(str);
            while (localResultSet.next()){
                String column = localResultSet.getString(1);
              /*  String type = localResultSet.getString(2);
                String comment = localResultSet.getString(3);
                String numpre = localResultSet.getString(4);
                String scale = localResultSet.getString(5);
                String len = localResultSet.getString(6);*/
                columns.add(column);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return  columns;
    }
}
